package com.fishbone.mybatisplus.injector.method;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.fishbone.mybatisplus.injector.ExtendSqlMethodEnum;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * @ClassName ExtendSqlMethod
 * @Author huyaxi
 * @DATE 2020/4/27 13:48
 */
public class ExtendSqlMethod extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        ExtendSqlMethodEnum sqlMethodEnum = ExtendSqlMethodEnum.EXTEND_SQL_METHOD;
        String sql = String.format(sqlMethodEnum.getSql(), tableInfo.getTableName(), tableInfo.getKeyColumn(), tableInfo.getKeyProperty());
        SqlSource sqlSource = this.languageDriver.createSqlSource(this.configuration, sql, Object.class);
        return this.addSelectMappedStatementForTable(mapperClass,sqlMethodEnum.getMethod(),sqlSource,tableInfo);
    }
}
